package cn.wolfcode.car.advice;

import cn.wolfcode.car.exception.AppointmentException;
import com.alibaba.fastjson.JSON;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import result.JsonResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.annotation.Annotation;

/**
 * 捕获所有异常并处理
 * 只要异常能够传递到controller 并由controller抛出就可以捕获
 */
@ControllerAdvice

public class MyControllerAdvice {

    @ExceptionHandler(Exception.class)
    public String Exception(Exception e, HandlerMethod method, HttpServletResponse response) throws IOException {
        System.err.println("==========");
        e.printStackTrace();
        System.err.println("==========");
        Annotation responseBody = method.getMethodAnnotation(ResponseBody.class);
        if (responseBody == null) {
            return "common/error";
        } else {
            JsonResult error = JsonResult.error("你啊,没有权限");
            String str = JSON.toJSONString(error);
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().write(str);
            return null;
        }
    }

    /**
     * 指定当前函数处理当前AuthorizationException类型的异常
     *
     * @return
     */
    @ExceptionHandler(UnauthorizedException.class)
    public String UnauthorizedException(UnauthorizedException e, HandlerMethod method, HttpServletResponse response) throws IOException {
        System.err.println("==========");
        e.printStackTrace();
        System.err.println("==========");
        Annotation responseBody = method.getMethodAnnotation(ResponseBody.class);
        if (responseBody == null) {
            return "common/nopermission";
        } else {
            JsonResult error = JsonResult.error("你啊,没有权限");
            String str = JSON.toJSONString(error);
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().write(str);
            return null;
        }
    }

    @ExceptionHandler(UnknownAccountException.class)
    @ResponseBody
    public Object UnknownAccountException(UnknownAccountException e, HttpServletResponse response) {
        System.err.println("==========");
        e.printStackTrace();
        System.err.println("==========");
        return JsonResult.error("账户不存在");
    }

    @ExceptionHandler(IncorrectCredentialsException.class)
    @ResponseBody
    public Object IncorrectCredentialsException(IncorrectCredentialsException e) {
        System.err.println("==========");
        e.printStackTrace();
        System.err.println("==========");
        return JsonResult.error("密码错误");
    }

    @ExceptionHandler(AppointmentException.class)
    public Object AppointmentExceptionException(AppointmentException e, Model model) {
        model.addAttribute("msg", e.getMessage());
        return "common/errorplus";
    }

}
